/*
 * Copyright 2013 JROSE
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.jrose.service.webapp.lifecycle;

import org.jrose.core.util.ApplicationContextProvider;
import org.jrose.service.loader.CareerGroupLoader;
import org.jrose.service.loader.CareerLoader;
import org.jrose.service.loader.InitialAttributeLoader;
import org.jrose.service.loader.InitializingLoader;
import org.jrose.service.loader.LineupLoader;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;

import javax.inject.Named;
import java.util.ArrayList;
import java.util.List;

/**
 * Implementation for executing post-processes of service webapp startup.
 *
 * @author Xiaohai Zhang
 * @since 2013-08-27 11:17
 */
@Named
public class ServiceStartup extends ApplicationContextProvider implements ApplicationListener<ContextRefreshedEvent> {
    @Override
    public void onApplicationEvent(ContextRefreshedEvent event) {
        if (event.getApplicationContext() != getApplicationContext()) {
            return;
        }

        List<Class<?>> loaderClasses = new ArrayList<>();
        loaderClasses.add(CareerLoader.class);
        loaderClasses.add(CareerGroupLoader.class);
        loaderClasses.add(LineupLoader.class);
        loaderClasses.add(InitialAttributeLoader.class);

        // trigger all configuration loading
        for (Class<?> loaderClass : loaderClasses) {
            Object loader = getBean(loaderClass);
            if (loader instanceof InitializingLoader) {
                ((InitializingLoader) loader).initializeLoader();
            }
        }
    }
}
